package com.dyz.LeetCodeforSwordFingerOffer;


import java.util.Stack;

/**
 * @author: daiyizheng
 * @date: 2021/7/4 11:21
 * @description:
 */
public class ValidateStackSequences31 {
    public static void main(String[] args) {
        int[] a = new int[]{1,2,3,4,5};
        int[] b = new int[]{4,5,3,2,1};
        new ValidateStackSequences31().validateStackSequences(a,b);
    }
    public boolean validateStackSequences(int[] pushed, int[] popped) {
        Stack<Integer> stack = new Stack<>();//辅助栈，专门存放push的数
        int i = 0;
        for(int num: pushed){
            stack.push(num);//每次循环放入stack中
            while (!stack.isEmpty() && stack.peek()==popped[i]){
                stack.pop();//如果popped顶部值与stack相同，pop掉
                i++;
            }
        }
        return stack.isEmpty();
    }
}
